package example;

import bean.Dog;
import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

public class ExtendsTest {
    @Test
    public void testExtends() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<Dog> dogClass = Dog.class;

        Field[] fields = dogClass.getDeclaredFields();
        fields[0].setAccessible(true);
        Arrays.stream(fields).forEach(System.out::println);

        Constructor<Dog> declaredConstructor = dogClass.getDeclaredConstructor();
        Dog dog = declaredConstructor.newInstance();
        Arrays.stream(fields).forEach(field -> {
            field.setAccessible(true);
            if (field.getType() == String.class){
                try {
                    field.set(dog, "haha");
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }else if (field.getType() == int.class){
                try {
                    field.set(dog, 1);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println(dog);
        System.out.println(dog.getAge());
    }
}
